<template>
  <a-modal
    title="编辑库位"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="所属仓库"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择所属仓库" v-decorator="['storehouseCode', {rules: [{ required: true, message: '请选择所属仓库！' }]}]" @change="handleChangeStorehouse">
            <a-select-option v-for="(item,index) in storehouseData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="所属库区"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择所属库区" v-decorator="['storehouseAreaCode', {rules: [{ required: true, message: '请选择所属库区！' }]}]">
            <a-select-option v-for="(item,index) in storehouseAreaData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="库位编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入库位编号" v-decorator="['code', {rules: [{required: true, message: '请输入库位编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="是否启用"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择是否启用" v-decorator="['isEnable', {rules: [{ required: true, message: '请选择是否启用！' }]}]">
            <a-select-option v-for="(item,index) in isEnableData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { storehouseLocationEdit } from '@/api/modular/main/base/storehouselocation/storehouseLocationManage'
  import { storehouseAreaList } from '@/api/modular/main/base/storehousearea/storehouseAreaManage'
  import { storehouseList } from '@/api/modular/main/base/storehouse/storehouseManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        isEnableData: [],
        storehouseData: [],
        storehouseAreaData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        const isEnableOption = this.$options
        this.isEnableData = isEnableOption.filters['dictData']('whether')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: this.$praseStrEmpty(record.id),
              code: this.$praseStrEmpty(record.code),
              storehouseCode: this.$praseStrEmpty(record.storehouseCode),
              storehouseAreaCode: this.$praseStrEmpty(record.storehouseAreaCode),
              isEnable: this.$praseStrEmpty(record.isEnable) + '',
              remark: this.$praseStrEmpty(record.remark)
            }
          )
        }, 100)
        this.getStorehouseList()
      },
      /**
       * 获取仓库列表
       */
      getStorehouseList () {
        storehouseList().then((res) => {
          if (res.success) {
            this.storehouseData = res.data
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      /**
       * 获取库区列表
       */
      getStorehouseAreaList () {
        storehouseAreaList().then((res) => {
          if (res.success) {
            this.storehouseAreaData = res.data
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      handleChangeStorehouse (value) {
        this.form.setFieldsValue(
          {
            storehouseAreaCode: ''
          }
        )
        storehouseAreaList({ 'storehouseCode': value }).then((res) => {
          if (res.success) {
            this.storehouseAreaData = res.data
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            storehouseLocationEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败:')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
